package com.kdzy.lshy.socity_server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kdzy.lshy.socity_server.common.Constants;
import com.kdzy.lshy.socity_server.common.ExceptionDetailMessage;
import com.kdzy.lshy.socity_server.common.ResultMap;
import com.kdzy.lshy.socity_server.repository.BaseDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service(value = "departmentService")
public class DepartmentService extends BaseService {

    @Resource
    BaseDao baseDao;

    private static final Logger logger = LoggerFactory.getLogger(DepartmentService.class);


     // 部门查询
    public Object getAllByCondition(String param, HttpServletRequest request) {

        JSONObject json = JSON.parseObject(param);

        String status;
        String info;
        Object content;

        if (null == json) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (json.getInteger("visit_plat") == 0) { // 网页端
            systoken = json.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《部门查询》findDepartment()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String dwellerIdcard = null;
        String dwellerId = null;
        if (json.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken",  "dwellerIdcard", "dwellerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                dwellerIdcard = infoList.get(1); // 居民身份证号
                dwellerId = infoList.get(2); // 居民唯一标识
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《部门查询》findDepartment()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《部门查询》findDepartment()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }

        StringBuilder builder = new StringBuilder();
        builder.append(
                "select s.socity_name,d.id,d.dep_name,dl.url,dl.descs, d.is_used isUsed \n" +
                        "from socity s,socity_department sd ,departments d ,dep_logo dl\n" +
                        "where s.id = sd.soc_id \n" +
                        "and d.id = sd.dep_id \n" +
                        "and dl.dep_id = d.id");

        if (StringUtils.isNoneBlank(json.getString("id"))) {
            builder.append(" and s.id = '" + json.getString("id") + "'");
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }
        //部门数据是否使用
        if (StringUtils.isNoneBlank(json.getString("is_used"))) {
            builder.append(" and s.is_used = '" + json.getString("is_used") + "'");
        }
        //部门数据版本
        if (StringUtils.isNoneBlank(json.getString("version"))) {
            builder.append(" and s.version = '" + json.getString("version") + "'");
        }
        //部门访问类型
        if (StringUtils.isNoneBlank(json.getString("visit_type"))) {
            builder.append(" and s.visit_type = '" + json.getString("visit_type") + "'");
        }
        //社区数据是否使用
//        if (StringUtils.isNoneBlank(json.getString("is_used"))) {
//            builder.append(" and d.is_used = '" + json.getString("is_used") + "'");
//        }
        //社区数据版本
        if (StringUtils.isNoneBlank(json.getString("version"))) {
            builder.append(" and d.version = '" + json.getString("version") + "'");
        }
        //社区访问类型
        if (StringUtils.isNoneBlank(json.getString("visit_type"))) {
            builder.append(" and d.visit_type = '" + json.getString("visit_type") + "'");
        }
        logger.info(builder.toString());
        try {
            content = baseDao.queryForList(builder.toString());

            status = Constants.http_status_success;
            info = Constants.http_info_success;
        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }
        return ResultMap.creatReault(status, info, content);

    }

    //    部门业务查询
    public Object findByBizz(String param, HttpServletRequest request) {
        String status;
        String info;
        Object content;

        JSONObject json = JSON.parseObject(param);

        //**********************************************************
        if (null == json) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (!(json.getInteger("visit_plat") != 0)) { // 网页端
            systoken = json.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《部门业务查询》findByBizz()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String dwellerIdcard = null;
        String dwellerId = null;
        if (json.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken",  "dwellerIdcard", "dwellerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                dwellerIdcard = infoList.get(1); // 居民身份证号
                dwellerId = infoList.get(2); // 居民唯一标识
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《部门业务查询》findByBizz()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《部门业务查询》findByBizz()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //**********************************************************

        StringBuilder builder = new StringBuilder();

        builder.append(
                "SELECT\n" +
                        "\td.id dep_id,\n" +
                        "\td.dep_name,\n" +
                        "\tb.id bizz_id,\n" +
                        "\tb.bizz_name,\n" +
                        "\tbl.url,\n" +
                        "\tbl.descs,\n" +
                        "\tbl.descs,\n" +
                        "\n" +
                        "IF (\n" +
                        "\tb.ID IN (\n" +
                        "\t\tSELECT\n" +
                        "\t\t\tb.ID\n" +
                        "\t\tFROM\n" +
                        "\t\t\tbizz b\n" +
                        "\t\tWHERE\n" +
                        "\t\t\tb.ID NOT IN (\n" +
                        "\t\t\t\tSELECT\n" +
                        "\t\t\t\t\tbizz.bizz_parent_id\n" +
                        "\t\t\t\tFROM\n" +
                        "\t\t\t\t\tbizz\n" +
                        "\t\t\t\tWHERE\n" +
                        "\t\t\t\t\tbizz.bizz_parent_id IS NOT NULL\n" +
                        "\t\t\t\tAND bizz.bizz_parent_id != ''\n" +
                        "\t\t\t)\n" +
                        "\t\tAND b.bizz_has_parent = 0\n" +
                        "\t),\n" +
                        "\tfalse,\n" +
                        "\ttrue\n" +
                        ") hasChild\n" +
                        "FROM\n" +
                        "\tdepartments d\n" +
                        "RIGHT JOIN department_bizz db ON d.id = db.dep_id\n" +
                        "RIGHT JOIN bizz b ON db.bizz_id = b.id\n" +
                        "RIGHT JOIN bizz_logo bl ON bl.bizz_id = b.id\n" +
                        "WHERE\n" +
                        "\t1 = 1");


        if (StringUtils.isNoneBlank(json.getString("id"))) {
            builder.append(" and d.id = '" + json.getString("id") + "'");
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }
        //数据是否使用
        if (StringUtils.isNoneBlank(json.getString("is_used"))) {
            builder.append(" and d.is_used = '" + json.getString("is_used") + "'");
        }
        //数据版本
        if (StringUtils.isNoneBlank(json.getString("version"))) {
            builder.append(" and d.version = '" + json.getString("version") + "'");
        }
        //访问类型
        if (StringUtils.isNoneBlank(json.getString("visit_type"))) {
            builder.append(" and d.visit_type = '" + json.getString("visit_type") + "'");
        }
        try {
            content = baseDao.queryForList(builder.toString());
            status = Constants.http_status_success;
            info = Constants.http_info_success;
        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }

        map = ResultMap.creatReault(status, info, content);
        logger.info("查询主业务出参：" + map);
//        map.put("hasChild", hasChild);
        return map;

    }


    //部门子业务查询
    public Object findByBizzs(String param, HttpServletRequest request) {
        String status;
        String info;
        Object content;

        // *****************************************************************************

        if (StringUtils.isBlank(param)) { // 为空
            logger.info("参数为空");
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        JSONObject json = JSON.parseObject(param);

        //***********************************************************************************
        LinkedHashMap<Object, Object> map =null;

        String systoken = null;
        if (json.getInteger("visit_plat") == 0) { // 网页端
            systoken = json.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《部门子业务查询》findByBizzs()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String dwellerIdcard = null;
        String dwellerId = null;
        if (json.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken",  "dwellerIdcard", "dwellerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                dwellerIdcard = infoList.get(1); // 居民身份证号
                dwellerId = infoList.get(2); // 居民唯一标识
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《部门子业务查询》findByBizzs()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《部门子业务查询》findByBizzs()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //***********************************************************************************

        // 获取id
        String bizzId = json.getString("id");

        if (StringUtils.isBlank(bizzId)) { // 为空
            logger.info("参数为空");
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        // 判断该业务id是否包含子业务
        String hasChildBizz = "SELECT\n" +
                "\tb.ID\n" +
                "FROM\n" +
                "\tbizz b\n" +
                "WHERE\n" +
                "\tb.bizz_has_parent = 0\n" +
                "AND b.ID NOT IN(\n" +
                "\tSELECT\n" +
                "\t\tbizz.bizz_parent_id\n" +
                "\tFROM\n" +
                "\t\tbizz\n" +
                "\tWHERE\n" +
                "\t\tbizz.bizz_parent_id IS NOT NULL\n" +
                "\tAND bizz.bizz_parent_id != ''\n" +
                ")\n" +
                "AND b.ID = '" + bizzId + "'";

        List<Map<String, Object>> hasChildList = null;
        try {
            hasChildList = baseDao.queryForList(hasChildBizz);
        } catch (Exception e) {
            logger.error("查询数据库异常：" + ExceptionDetailMessage.getExceptionDetail(e));
            return ResultMap.creatReault(Constants.http_status_fail, Constants.http_info_fali, "后台处理异常");
        }

        if (hasChildList != null && hasChildList.size() > 0) { // 没有子业务
            // 直接查询业务流程及相关所需证件
            String resourceSql = "SELECT\n" +
                    "\tdep.ID dep_id,\n" +
                    "\tdep.dep_name,\n" +
                    "\tb.ID bizz_id,\n" +
                    "\tb.bizz_name bizz_name,\n" +
                    "\tr.ID res_id,\n" +
                    "\tr.res_name,\n" +
                    "\tr.res_demo_url,\n" +
                    "\tbr.res_num,\n" +
                    "\tbr.res_type,\n" +
                    "\tbr.res_rule,\n" +
                    "\tbr.note res_note,\n" +
                    "\tbp.bizz_dec\n" +
                    "FROM\n" +
                    "\tbizz b,\n" +
                    "\tbizz_procee bp,\n" +
                    "\tbizz_resource br,\n" +
                    "\tresource r,\n" +
                    "\tdepartments dep,\n" +
                    "\tdepartment_bizz depb\n" +
                    "WHERE\n" +
                    "\tbp.bizz_id = br.bizz_id\n" +
                    "AND br.res_id = r.ID\n" +
                    "AND b.ID = br.bizz_id\n" +
                    "AND b.ID = depb.bizz_id\n" +
                    "AND depb.dep_id = dep.ID\n" +
                    "AND br.is_used = 0\n" +
                    "AND br.bizz_id = '"+bizzId+"'\n" +
                    "\n";
            List<Map<String, Object>> resourceList = null;
            try {
                resourceList = baseDao.queryForList(resourceSql);
                LinkedHashMap<Object, Object> resMap = ResultMap.creatReault(Constants.http_status_success, Constants.http_info_success, resourceList);
                resMap.put("hasChild", false); // 没有子业务
                logger.info("没有子业务成功：" + resMap);
                return resMap;
            } catch (Exception e) {
                logger.error("查询数据库异常：" + ExceptionDetailMessage.getExceptionDetail(e));
                return ResultMap.creatReault(Constants.http_status_fail, Constants.http_info_fali, "后台处理异常");
            }

        }

        // *****************************************************************************

        StringBuilder builder = new StringBuilder();

        builder.append(
                "select d.id dep_id,d.dep_name,bf.id bizz_id,bf.bizz_name," +
                        "bl.descs,bl.url,b.id chl_id,b.bizz_name chl_name\n" +
                        "from bizz b,bizz bf,department_bizz db,departments d,bizz_logo bl\n" +
                        "where b.bizz_parent_id = bf.id and bl.bizz_id = bf.id\n" +
                        "and b.bizz_parent_id = db.bizz_id and db.dep_id = d.id\n");


        if (StringUtils.isNoneBlank(json.getString("id"))) {
            builder.append(" and b.bizz_parent_id = '" + json.getString("id") + "'");
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }
        //数据是否使用
        if (StringUtils.isNoneBlank(json.getString("is_used"))) {
            builder.append(" and b.is_used = '" + json.getString("is_used") + "'");
        }
        //数据版本
        if (StringUtils.isNoneBlank(json.getString("version"))) {
            builder.append(" and b.version = '" + json.getString("version") + "'");
        }
        //访问类型
        if (StringUtils.isNoneBlank(json.getString("visit_type"))) {
            builder.append(" and b.visit_type = '" + json.getString("visit_type") + "'");
        }
        try {
            content = baseDao.queryForList(builder.toString());
            status = Constants.http_status_success;
            info = Constants.http_info_success;
            LinkedHashMap<Object, Object> sucessMap = ResultMap.creatReault(status, info, content);
            sucessMap.put("hasChild", true); // 有子业务
            logger.info("有子业务成功：" + sucessMap);
            return sucessMap;
        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }
        return ResultMap.creatReault(status, info, content);
    }


    // 业务证件查询t;
    public Object findByTestify(String param, HttpServletRequest request) {

        String status;
        String info;
        Object content;

        if (StringUtils.isBlank(param)) { // 为空
            logger.info("参数为空");
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        JSONObject json = JSON.parseObject(param);

        //***********************************************************************************
        LinkedHashMap<Object, Object> map =null;

        String systoken = null;
        if (json.getInteger("visit_plat") == 0) { // 网页端
            systoken = json.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《业务证件查询》findByTestify()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String dwellerIdcard = null;
        String dwellerId = null;
        if (json.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken",  "dwellerIdcard", "dwellerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                dwellerIdcard = infoList.get(1); // 居民身份证号
                dwellerId = infoList.get(2); // 居民唯一标识
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《业务证件查询》findByTestify()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《业务证件查询》findByTestify()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //***********************************************************************************

        try{
            String id = json.getString("id");

            if (StringUtils.isBlank(id)) { // 为空
                logger.info("参数为空");
                status = Constants.http_status_fail;
                info = Constants.http_info_fali;
                content = "参数为空";
                return ResultMap.creatReault(status, info, content);
            }

            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");
            String abc = jedis.get(id);

            if (StringUtils.isBlank(abc)) { // 为空，查询数据库
                StringBuilder builder = new StringBuilder();
                // 可能是redis连接失败
                String sql = "SELECT\n" +
                        "\tdep.id dep_id,\n" +
                        "\tdep.dep_name,\n" +
                        "\tbizz.bizz_parent_id fbizz_id,\n" +
                        "\tbf.bizz_name fbizz_name,\n" +
                        "\tbizz.id bizz_id,\n" +
                        "\tbizz.bizz_name,\n" +
                        "\tr.id res_id,\n" +
                        "\tr.res_name,\n" +
                        "\tr.res_demo_url,\n" +
                        "\tbr.res_num,\n" +
                        "\tbr.res_type,\n" +
                        "\tbr.res_rule,\n" +
                        "\tbr.note res_note,\n" +
                        "  bp.bizz_dec\n" +
                        "FROM\n" +
                        "\tbizz,\n" +
                        "\tbizz bf,\n" +
                        "\tbizz_resource br,\n" +
                        "\tresource r,\n" +
                        "\tdepartments dep,\n" +
                        "\tdepartment_bizz db,\n" +
                        "\tbizz_procee bp\n" +
                        "WHERE\n" +
                        "\tbizz.bizz_parent_id = bf.id\n" +
                        "AND bf.id = db.bizz_id\n" +
                        "AND db.bizz_id = bf.id\n" +
                        "AND db.dep_id = dep.id\n" +
                        "AND br.bizz_id = bizz.ID\n" +
                        "AND br.res_id = r.id\n" +
                        "AND br.is_used = 0\n" +
                        "AND bp.bizz_id = bizz.ID";

                String id1 = (String) json.get("id");
                builder.delete(0, builder.toString().length());
                builder.append(" and bizz.id = '" + id1 + "'");
                sql += builder.toString();

                try {
                    List<Map<String, Object>> mapRes = baseDao.queryForList(sql);
                    status = Constants.http_status_success;
                    info = Constants.http_info_success;
                    logger.info("数据库查询成功：" + ResultMap.creatReault(status, info, mapRes));
                    return ResultMap.creatReault(status, info, mapRes);
                } catch (Exception e2) {
                    logger.error("查询数据库异常：" + ExceptionDetailMessage.getExceptionDetail(e2));

                    status = Constants.http_status_fail;
                    info = Constants.http_info_fali;
                    return ResultMap.creatReault(status, info, "后台处理异常");
                }
            }
            jedis.close();
            return abc;

        } catch (Exception e) {
            logger.error("jedis异常：" + ExceptionDetailMessage.getExceptionDetail(e));

            StringBuilder builder = new StringBuilder();
            // 可能是redis连接失败
            String sql = "SELECT\n" +
                    "\tdep.id dep_id,\n" +
                    "\tdep.dep_name,\n" +
                    "\tbizz.bizz_parent_id fbizz_id,\n" +
                    "\tbf.bizz_name fbizz_name,\n" +
                    "\tbizz.id bizz_id,\n" +
                    "\tbizz.bizz_name,\n" +
                    "\tr.id res_id,\n" +
                    "\tr.res_name,\n" +
                    "\tr.res_demo_url,\n" +
                    "\tbr.res_num,\n" +
                    "\tbr.res_type,\n" +
                    "\tbr.res_rule,\n" +
                    "\tbr.note res_note,\n" +
                    "  bp.bizz_dec\n" +
                    "FROM\n" +
                    "\tbizz,\n" +
                    "\tbizz bf,\n" +
                    "\tbizz_resource br,\n" +
                    "\tresource r,\n" +
                    "\tdepartments dep,\n" +
                    "\tdepartment_bizz db,\n" +
                    "\tbizz_procee bp\n" +
                    "WHERE\n" +
                    "\tbizz.bizz_parent_id = bf.id\n" +
                    "AND bf.id = db.bizz_id\n" +
                    "AND db.bizz_id = bf.id\n" +
                    "AND db.dep_id = dep.id\n" +
                    "AND br.bizz_id = bizz.ID\n" +
                    "AND br.res_id = r.id\n" +
                    "AND br.is_used = 0\n" +
                    "AND bp.bizz_id = bizz.ID";

            String id = (String) json.get("ID");
            builder.delete(0, builder.toString().length());
            builder.append(" and bizz.id = '" + id + "'");
            sql += builder.toString();

            try {
                List<Map<String, Object>> mapRes = baseDao.queryForList(sql);
                status = Constants.http_status_success;
                info = Constants.http_info_success;
                logger.info("数据库查询成功：" + ResultMap.creatReault(status, info, mapRes));
                return ResultMap.creatReault(status, info, mapRes);
            } catch (Exception e2) {
                logger.error("查询数据库异常：" + ExceptionDetailMessage.getExceptionDetail(e2));

                status = Constants.http_status_fail;
                info = Constants.http_info_fali;
                return ResultMap.creatReault(status, info, "后台处理异常");
            }
        }

    }

    /**
     * 业务证件查询并存入redis中
     */
//    public Object findByTestify(String param, HttpServletRequest request) {
//
//        JSONObject json = JSON.parseObject(param);
//        StringBuilder builder = new StringBuilder();
//
//        String status;
//        String info;
//        Object content;
//        List<Map<String, Object>> bizzIdList = null;
//
//        String bizzIdSql = "SELECT\n" +
//                "\tb.ID\n" +
//                "FROM\n" +
//                "\tbizz b\n" +
//                "WHERE\n" +
//                "\tb.bizz_parent_id IS NOT NULL\n" +
//                "AND b.bizz_parent_id != ''\n" +
//                "UNION ALL\n" +
//                "\tSELECT\n" +
//                "\t\tbf.ID\n" +
//                "\tFROM\n" +
//                "\t\tbizz bf\n" +
//                "\tWHERE\n" +
//                "\t\tbf.ID NOT IN (\n" +
//                "\t\t\tSELECT\n" +
//                "\t\t\t\tbizz.bizz_parent_id\n" +
//                "\t\t\tFROM\n" +
//                "\t\t\t\tbizz\n" +
//                "\t\t\tWHERE\n" +
//                "\t\t\t\tbizz_parent_id IS NOT NULL\n" +
//                "\t\t\tAND bizz_parent_id != ''\n" +
//                "\t\t)\n" +
//                "\tAND bf.bizz_has_parent = 0\n";
//
//        bizzIdList = baseDao.queryForList(bizzIdSql);
//
//        Jedis jedis = new Jedis("140.143.210.101", 6379);
//    jedis.auth("q1w2e3r4t5");
//        int i = 0;
//        for (Map map: bizzIdList) {
//            System.out.println("第 " + i + " 个");
//
//            String sql = "SELECT\n" +
//                    "\tdep.id dep_id,\n" +
//                    "\tdep.dep_name,\n" +
//                    "\tbizz.bizz_parent_id fbizz_id,\n" +
//                    "\tbf.bizz_name fbizz_name,\n" +
//                    "\tbizz.id bizz_id,\n" +
//                    "\tbizz.bizz_name,\n" +
//                    "\tr.id res_id,\n" +
//                    "\tr.res_name,\n" +
//                    "\tr.res_demo_url,\n" +
//                    "\tbr.res_num,\n" +
//                    "\tbr.res_type,\n" +
//                    "\tbr.res_rule,\n" +
//                    "\tbr.note res_note,\n" +
//                    "  bp.bizz_dec\n" +
//                    "FROM\n" +
//                    "\tbizz,\n" +
//                    "\tbizz bf,\n" +
//                    "\tbizz_resource br,\n" +
//                    "\tresource r,\n" +
//                    "\tdepartments dep,\n" +
//                    "\tdepartment_bizz db,\n" +
//                    "\tbizz_procee bp\n" +
//                    "WHERE\n" +
//                    "\tbizz.bizz_parent_id = bf.id\n" +
//                    "AND bf.id = db.bizz_id\n" +
//                    "AND db.bizz_id = bf.id\n" +
//                    "AND db.dep_id = dep.id\n" +
//                    "AND br.bizz_id = bizz.ID\n" +
//                    "AND br.res_id = r.id\n" +
//                    "AND br.is_used = 0\n" +
//                    "AND bp.bizz_id = bizz.ID";
//
//            String id = (String) map.get("ID");
//            builder.delete(0, builder.toString().length());
//            builder.append(" and bizz.id = '" + id + "'");
//            sql += builder.toString();
//            content = baseDao.queryForList(sql);
//            jedis.set(id, JSON.toJSONString(ResultMap.creatReault("0", "success", content)));
//            jedis.persist(id);
//            i++;
//        }
//
//        return null;
//    }

}